<template>
  <div ref="sectionChart" style="width: 100%; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';
import {sectionDistribution} from "@/api/system/config";

export default {
  mounted() {
    this.initSectionChart();
  },
  data(){
    return{
      data:[]
    }
  },
  methods: {
    async initSectionChart() {
      try {
        await sectionDistribution().then(response => {
          this.data  = response.data
        });

        const option = {
          title: {
            text: '板块帖子分布',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          series: [
            {
              name: '帖子数量',
              type: 'pie',
              radius: '50%',
              data: this.data.map(item => ({
                value: item.postCount,
                name: item.sectionName
              }))
            }
          ]
        };

        const chart = echarts.init(this.$refs.sectionChart);
        chart.setOption(option);
      } catch (error) {
        console.error('获取板块帖子分布数据失败:', error);
      }
    }
  }
};
</script>
